package com.my.screenadapter;

import android.content.ComponentName;
import android.content.pm.PackageManager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.my.screenadapter.View.TimeDialog;

public class MainViewsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_views);
      initView();
    }

    PackageManager pm ;
    private void initView() {
        pm = getPackageManager();
        //Android之动态更换桌面图标
        //http://blog.csdn.net/qq_16628781/article/details/69054325
        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        Button button = new Button(this);
        button.setText("切换icon1");
        Button button2 = new Button(this);
        button2.setText("切换icon2");
        ll.addView(button);
        ll.addView(button2);
        Button button3 = new Button(this);
        button3.setText("切换default");
        ll.addView(button3);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setIsEnableComponent(new ComponentName(getBaseContext(),"com.my.screenadapter.MainActivity_alias0"),false);
                setIsEnableComponent(new ComponentName(getBaseContext(),"com.my.screenadapter.MainActivity_alias2"),false);
                setIsEnableComponent(new ComponentName(getBaseContext(),"com.my.screenadapter.MainActivity_alias1"),true);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setIsEnableComponent(new ComponentName(getBaseContext(),"com.my.screenadapter.MainActivity_alias0"),false);
                setIsEnableComponent(new ComponentName(getBaseContext(),"com.my.screenadapter.MainActivity_alias1"),false);
                setIsEnableComponent(new ComponentName(getBaseContext(),"com.my.screenadapter.MainActivity_alias2"),true);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setIsEnableComponent(new ComponentName(getBaseContext(),"com.my.screenadapter.MainActivity_alias0"),true);
                setIsEnableComponent(new ComponentName(getBaseContext(),"com.my.screenadapter.MainActivity_alias1"),false);
                setIsEnableComponent(new ComponentName(getBaseContext(),"com.my.screenadapter.MainActivity_alias2"),false);
            }
        });
        findViewById(R.id.tvtime).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimeDialog(MainViewsActivity.this, (Button) findViewById(R.id.tvtime));
            }
        });

    }



    /**
     * 启动组件
     *
     * @param componentName 组件名
     */
    private void setIsEnableComponent(ComponentName componentName,boolean isEnable) {
        if (isEnable){
            //此方法用以启用和禁用组件，会覆盖Androidmanifest文件下定义的属性
            pm.setComponentEnabledSetting(componentName,
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
        }else {
            //此方法用以启用和禁用组件，会覆盖Androidmanifest文件下定义的属性
            pm.setComponentEnabledSetting(componentName,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
        }
    }

}
